package org.stone.project.service;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stone.project.mapper.DemoMapper;
import org.stone.project.po.Demo;
import org.theSeed.base.exp.ServiceExp;
import org.theSeed.springDynamicDatasource.utils.DynamicDataSourceUtil;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class DemoService {
    @Autowired
    private DemoMapper bean;

    @Autowired
    private List<DataSource> dataSources;

    public void test1(){
        for (int j = 0; j < 100; j++) {
            List<Demo> collect = Stream.iterate(0, i -> i++)
                    .limit(10)
                    .map(item->{
                        Demo demo = new Demo();
                        demo.setName("测试名称"+ UUID.fastUUID().hashCode());
                        return demo;
                    })
                    .collect(Collectors.toList());
            bean.insertBatchSomeColumn(collect);
        }
        log.info("插入完成");
    }

    @Transactional
    public void test2(){
        for (int j = 0; j < 10; j++) {
            List<Demo> collect = Stream.iterate(0, i -> i++)
                    .limit(10)
                    .map(item->{
                        Demo demo = new Demo();
                        demo.setName("测试名称"+ UUID.fastUUID().hashCode());
                        return demo;
                    })
                    .collect(Collectors.toList());
            bean.insertBatchSomeColumn(collect);
//            if (j == 9)
//                throw new ServiceExp("不插入了");
        }
        log.info("插入完成");
    }


    @Transactional
    public void test3(){
//        强制走主库
//        HintManager.getInstance().setMasterRouteOnly();
        for (int i = 0; i < 10; i++) {
            Demo demo2 = new Demo();
            demo2.setName("测试名称"+ UUID.fastUUID().hashCode());
            bean.insertBatchSomeColumn(Arrays.asList(demo2));
        }
        Demo demo = bean.selectById("5972a3d89b94db6c43352562ca2bf923");
        List<Demo> demos = bean.selectList(Wrappers.lambdaQuery());
        TransactionTypeHolder.set(TransactionType.XA);
        log.info("数据结果为{}", JSONUtil.toJsonStr(demo));
        if (demo.getName().equals("测试名称_指定")){
            Demo demo2 = new Demo();
            demo2.setName("测试名称"+ UUID.fastUUID().hashCode());
            bean.insertBatchSomeColumn(Arrays.asList(demo2));
        }

    }

    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void test4(){
//        强制走主库
//        HintManager.getInstance().setMasterRouteOnly();
        for (int i = 0; i < 10; i++) {
            Demo demo2 = new Demo();
            demo2.setName("测试名称"+ UUID.fastUUID().hashCode());
            bean.insertBatchSomeColumn(Arrays.asList(demo2));

            if (i == 9){
                throw new ServiceExp();
            }
        }
    }
}
